package com.springboot.demo.common.utils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

@SuppressWarnings("all")
public class HttpRequest {
	
	/**
	 * @MethodName:qztPost
	 * @Author JuoChiu
	 * @Date 2017年12月18日 下午3:10:33
	 * @Description:这里用一句话描述这个方法的作用
	 * @param map
	 * @return
	 * @ReturnType String
	 */
	public static String qztPost(Map<String, String> map){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String result=null;
        try {
            HttpPost httppost = new HttpPost("http://localhost:8080/api/chb/excel/upload_excel");
            httppost.setHeader("Content-Type", "application/json");
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
           
            for (Map.Entry<String, String> entry : map.entrySet()) {
                pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
            }

            HttpEntity entity = new UrlEncodedFormEntity(pairs, "utf-8");
            httppost.setEntity(entity);
            CloseableHttpResponse response = httpclient.execute(httppost);

            /*try {
                System.out.println(response.getStatusLine());
                System.out.println(response.getFirstHeader("Location")+"标题为这个!!!");
                result= response.getFirstHeader("Location").getValue();
                result = java.net.URLDecoder.decode(result,"UTF-8");
            }  finally {
                response.close();
            }*/
        } catch (IOException e) {
            e.printStackTrace();
            try {
                httpclient.close();
            } catch (IOException e1) {
                e.printStackTrace();
            }
        }
        return result;
    }
	
	/**
	 * @MethodName:qztPost
	 * @Author JuoChiu
	 * @Date 2017年12月18日 下午3:09:57
	 * @Description:这里用一句话描述这个方法的作用
	 * @param akb
	 * @return
	 * @ReturnType String
	 */
	public static String qztPost(String akb){
	    CloseableHttpClient httpclient = HttpClients.createDefault();
	    String result=null;
	    try {
	        HttpPost httppost = new HttpPost("http://localhost:8080/api/chb/excel/upload_excel");
	        httppost.setHeader("Content-Type", "application/json");
	        List<StringEntity> pairs = new ArrayList<StringEntity>();
	        httppost.setEntity(new StringEntity(akb, Charset.forName("UTF-8")));
	        CloseableHttpResponse response = httpclient.execute(httppost);

	        try {
	            result= EntityUtils.toString(response.getEntity());
	        }  finally {
	            response.close();
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	        try {
	            httpclient.close();
	        } catch (IOException e1) {
	            e.printStackTrace();
	        }
	    }
	    return result;
	}
}
